package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Lesson;
import com.ruoyi.system.service.LessonService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/lesson")
public class LessonController extends BaseController {

    @Resource
    private LessonService lessonService;

    /**
     * 分页查询
     *
     * @param lesson      筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo list(Lesson lesson) {
        startPage();
        List<Lesson> lessons = lessonService.queryAll(lesson);
        return getDataTable(lessons);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "{lessonId}")
    public AjaxResult queryById(@PathVariable("lessonId") Long id) {
        return success(lessonService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param lesson 实体
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Lesson lesson){
        lesson.setCreateBy(getUsername());
        lesson.setCreateTime(DateUtils.getNowDate());
        return success(lessonService.insert(lesson));
    }

    /**
     * 编辑数据
     *
     * @param lesson 实体
     * @return 编辑结果
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Lesson lesson) {
        lesson.setUpdateBy(getUsername());
        lesson.setUpdateTime(DateUtils.getNowDate());
        return success(lessonService.update(lesson));
    }

    /**
     * 删除数据
     *
     * @param lessonId 主键
     * @return 删除是否成功
     */
    @DeleteMapping(value = "{lessonId}")
    public AjaxResult deleteById(@PathVariable String lessonId) {
        return success(lessonService.deleteById(Long.valueOf(lessonId)));
    }

    /**
     * 通过班级id查询该班级学生信息
     * @param id
     * @return
     */
    @GetMapping(value = "/st/{deptId}")
    public AjaxResult queryStudentByDept(@PathVariable("deptId") Long id){
        return success(lessonService.queryStudentByDept(id));
    }

}
